//
//  APPLEUserAccountViewModel.swift
//  123
//
//  Created by pangjiaxing on 15/12/30.
//  Copyright © 2015年 pangjiaxing. All rights reserved.
//

import UIKit

var WB_APPKEY = "956193458"
var WB_REGIRECTURI = "http://www.itheima.com/"
var WB_SECRET = "1c397c063bbe267c4ccac42b30ff8157"

class APPLEUserAccountViewModel: NSObject {
     static let sharedViewModel:APPLEUserAccountViewModel = APPLEUserAccountViewModel()
    override init() {
        super.init()
        userAccount = self.getUserAccount()
    }
    var userAccount:APPLEUserAccount?
    var isExpiresDate:Bool {
        if let expiresDate = userAccount?.expiresDate{
            let nowDate = NSDate()
            if nowDate.compare(expiresDate) == .OrderedAscending{
                return false
            }
        }
        return true
    }
    var accessToken:String?{
        return userAccount?.access_token
    }
    var userLogin:Bool {
        
        if accessToken != nil && isExpiresDate == false{
            return true
        }
        return false
    }
    
   
    
    
   
    
    func loadAccessToken(code:String,compeleted:(isSuccess:Bool)->()){
//            let urlString = "https://api.weibo.com/oauth2/access_token"
//            let params = [
//                "client_id": WB_APPKEY,
//                "client_secret": WB_SECRET,
//                "grant_type": "authorization_code",
//                "code": code,
//                "redirect_uri": WB_REGIRECTURI
//            ]
        APPLENetworkTools.sharedTools.loadAccessToken("authorization_code") { (response, error) -> () in
            if error != nil {
                compeleted(isSuccess: false)
                return
            }else{
                guard let dict = response as? [String:AnyObject]  else {
                    print("数据解析失败")
                    compeleted(isSuccess: false)
                    return
                }
                print("数据解析成功")
                let account = APPLEUserAccount(dict: dict)
                
                self.loadUserAccount(account, compeleted: compeleted)
            }
        }
        }
//        APPLENetworkTools.sharedTools.request(APPLERequestMethod.POST, urlstring: urlString, paraneter: params) { (response, error) -> () in
//            
//            
//        }
//    
    func loadUserAccount(account:APPLEUserAccount,compeleted:(isSuccess:Bool)->()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token": account.access_token!,
            "uid": account.uid!
        ]
        APPLENetworkTools.sharedTools.request(urlstring: urlString, paraneter: params) { (response, error) -> () in
            if error != nil {
                print(error)
                compeleted(isSuccess: false)
                return
            }else{
                guard let dict = response as? [String:AnyObject] else {
                    compeleted(isSuccess: false)
                    return
                }
                account.avatar_large = dict["avatar_large"] as? String
                account.screen_name = dict["screen_name"] as? String
                
                self.userAccount = account
                
                self.saveAccount(account)
                
                compeleted(isSuccess: true)
            }
        }
    }
}

/// 归档路径
private var archivePath = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first! as NSString).stringByAppendingPathComponent("useraccount.archive")

extension APPLEUserAccountViewModel {
    func saveAccount(account: APPLEUserAccount) {
        print(archivePath)
        // 归档
        NSKeyedArchiver.archiveRootObject(account, toFile: archivePath)
    }
    func getUserAccount() -> APPLEUserAccount? {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as? APPLEUserAccount
    }
}